package com.lagou.rocketmq.task.dao;

import com.lagou.rocketmq.task.model.entity.SeckillingProductDO;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;


public interface ProductDao extends JpaRepository<SeckillingProductDO, Integer> {

  /**
   * 扣减商品库存
   *
   * @param productId
   * @return
   */
  @Transactional
  @Modifying
  @Query(value = "update lagou_product set inventory = inventory-1" +
      " where id = ?1 and inventory > 0", nativeQuery = true)
  int decrProductInventory(Integer productId);


  /**
   * 增加商品库存
   *
   * @param productId
   * @return
   */
  @Transactional
  @Modifying
  @Query(value = "update lagou_product set inventory = inventory+1" +
      " where id = ?1", nativeQuery = true)
  int incrProductInventory(Integer productId);
}
